package com.xc.utils.task;

import com.xc.service.IUserService;
import com.xc.utils.DateTimeUtil;
import com.xc.utils.stock.BuyAndSellUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.Date;

/**
 *  强制平仓 定时任务
 * @author mipha
 * @version 1.0
 * @date 2019/3/9 11:28 PM
 */
@Component
@Slf4j
public class ForceSellTask {

    @Autowired
    IUserService iUserService;

    private static final String am_begin = "9:30";
    private static final String am_end = "11:30";
    private static final String pm_begin = "13:00";
    private static final String pm_end = "15:00";
                                                
    //每个 2 分钟执行一次       测试
//    @Scheduled(cron = "0 */2 * * * ?")
            

    // 周一到周五 的9点到15点 每三分钟执行一次   正式
    @Scheduled(cron = "0 0/3 9-15 ? * MON-FRI")
    public void banlanceUserPositionTaskV1() {
        boolean am = false;
        boolean pm = false;
        try {
            am = BuyAndSellUtils.isTransTime(am_begin,am_end);
            pm = BuyAndSellUtils.isTransTime(pm_begin,pm_end);
        }catch (Exception e){
            log.error("执行定时任务出错，e = {}" , e);
        }

        log.info("当前 am = {}  pm = {}" , am , pm);
        if(am || pm){
           log.info("=====扫描用户持仓执行，当前时间 {} =====",DateTimeUtil.dateToStr(new Date()));
           dotask();
           log.info("=====扫描用户持仓结束，当前时间 {} =====",DateTimeUtil.dateToStr(new Date()));
        }else {
           log.info("当前时间不为周一至周五，或者不在交易时间内，不执行（强平）定时任务");
        }

    }


    public void dotask(){
        iUserService.ForceSellTask();
    }


    public static void main(String[] args) {
//        StockListVO stockListVO = SinaStockApi
//                .assembleStockListVO(SinaStockApi.getSinaStock("sz002513"));
//
//        System.out.println("昨收 ： " + stockListVO.getPreclose_px());
//
//
//        BigDecimal zsPrice = new BigDecimal(stockListVO.getPreclose_px());
//        //  涨停价 = 6.97 * 0.1 + 6.97 涨停价需要四舍五入
//        BigDecimal ztPrice = zsPrice.multiply(new BigDecimal("0.1")).add(zsPrice);
//        ztPrice = ztPrice.setScale(2,BigDecimal.ROUND_HALF_UP);
//        BigDecimal chaPrice = ztPrice.subtract(zsPrice);
//        BigDecimal ztRate = chaPrice.multiply(new BigDecimal("100"))
//                .divide(zsPrice,2, BigDecimal.ROUND_HALF_UP);
//
//        System.out.println(new BigDecimal("9.94").compareTo(ztRate));
//
//        ztRate = ztRate.negate();
//
//        System.out.println(ztRate);
//
//        System.out.println(new BigDecimal("-9.94").compareTo(ztRate));

        double at = 10.07;
        System.out.println(new BigDecimal("10.07").compareTo(new BigDecimal(at)));
        System.out.println(new BigDecimal("10.07").compareTo(new BigDecimal(String.valueOf(at))));
    }


}
